﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.Config;
using Kovan.Model.Parameter.Config;
using System.Linq.Expressions;
using Kovan.Library.Extensions;

namespace Kovan.DAL.Config
{
    public class DPicGroup : IDAL<tb_picgroup>
    {

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<tb_picgroup> GetList(PPicGroup param)
        {
            using (var db = new MysqlContext())
            {
                Expression<Func<tb_picgroup, bool>> wh = c => c != null;

                if (!string.IsNullOrEmpty(param.name))
                {
                    wh = wh.And(c => c.name.Contains(param.name));
                }

                var query = db.tb_picgroup.OrderByDescending(c => c.picgroup_id.Value).AsQueryable();

                //总条数
                param.TotalRows = query.Where(wh).Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query = query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                query = query.Where(wh);

                return param.TotalRows > 0 ? query.ToList() : null;
            }
        }
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public tb_picgroup GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.tb_picgroup.FirstOrDefault(c => c.picgroup_id == id);
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(tb_picgroup model)
        {
            using (var db = new MysqlContext())
            {
                db.tb_picgroup.Add(model);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.tb_picgroup.FirstOrDefault(a => a.picgroup_id == id);
                db.tb_picgroup.Remove(model);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(tb_picgroup model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<tb_picgroup>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var pgid = long.Parse(id);
                    var model = db.tb_picgroup.FirstOrDefault(a => a.picgroup_id == pgid);
                    if (model != null)
                    {
                        db.tb_picgroup.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        public int BatchDelete(IEnumerable<string> ids, out string ukeys)
        {
            ukeys = string.Empty;
            var files = new List<string>();
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var pgid = long.Parse(id);
                    var model = db.tb_picgroup.FirstOrDefault(a => a.picgroup_id == pgid);
                    if (model != null)
                    {
                        db.tb_picgroup.Remove(model);
                        files.Add(model.ukey);
                    }
                }
                ukeys = string.Join(",", files);
                return db.SaveChanges();
            }
        }
    }
}
